<!DOCTYPE html>
<html>
<head>
    <title>序列号</title>
    <meta charset="utf-8">
    <!-- 指定以IE8的方式来渲染 -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="/css/jsherp.css"/>
    <script type="text/javascript" src="/js/jquery-1.8.0.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css"/>
    <link type="text/css" rel="stylesheet" href="/css/common.css"/>
    <link type="text/css" rel="stylesheet" href="/css/material.css"/>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common/outlook_in.js"></script>
    <script type="text/javascript" src="/js/common/common.js"></script>
    <script src="/js/common/jsherp.js"></script>
</head>
<body>
<!-- 数据显示table -->
<div id="tablePanel" class="easyui-panel" data-options="fit:true" style="padding:1px;top:300px;" title="序列号列表" iconCls="icon-list">
    <div class="box-body form-inline">
        <div class="form-group">
            <label class="control-label">药品名称：</label>
            <div class="control-inline">
                <input type="text" id="materialName_s" name="materialName_s" value="" maxlength="100" class="easyui-textbox width-90"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">序列号：</label>
            <div class="control-inline">
                <input type="text" id="serialNumber_s" name="serialNumber_s" value="" maxlength="100" class="easyui-textbox width-90"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">备注：</label>
            <div class="control-inline">
                <input type="text" id="searchRemark" name="searchRemark" value="" maxlength="100" class="easyui-textbox width-90"/>
            </div>
        </div>
        <div class="form-group">
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" id="searchBtn">查询</a>&nbsp;
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" id="searchResetBtn">重置</a>
        </div>
    </div>
    <table id="tableData" style="top:300px;border-bottom-color:#FFFFFF"></table>
    <input id="selectType" hidden="hidden" />
</div>

<div id="serialNumberDetailListDlg" class="easyui-dialog" style="width:900px;height:500px;padding:10px 20px" closed="true"
     modal="true" collapsible="false" closable="true">
    <table id="serialNumberTableData" style="top:50px;border-bottom-color:#FFFFFF"></table>
</div>

<div id="serialNumberDlg" class="easyui-dialog" style="padding:10px 20px" fit="true"
     closed="true" buttons="#dlg-buttons" modal="true" collapsible="false" closable="true">
    <form id="serialNumberFM" method="post" novalidate>
        <div id="tt" class="easyui-tabs" style="width:100%;height: 520px">
            <div title="基本信息" style="padding:20px;">
                <table>
                    <tr>
                        <td style="width:60px; height:30px;">药品名称</td>
                        <td style="padding:5px;width:300px;">
                            <input name="materialName" id="materialName" class="easyui-textbox" readonly="readonly"
                                   style="width: 180px;"/>
                            <a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id="lookForSelectMaterial">
                                <span class="l-btn-left"><span class="l-btn-text icon-search l-btn-icon-left" style="height: 20px;"></span></span></a>
                            <input name="materialId" id="materialId" type="hidden" />
                        </td>
                    </tr>
                    <tr>
                        <td style="width:80px;">序列号</td>
                        <td style="padding:5px;width:180px;">
                            <input name="serialNumberId" id="serialNumberId" type="hidden" />
                            <input name="serialNumber" id="serialNumber" class="easyui-textbox"
                                   data-options="required:true,validType:'length[1,30]'"
                                   style="width: 180px;"/>
                        </td>
                        <td style="width:200px;"></td>
                    </tr>
                    <tr>
                        <td style="height:30px;">备注</td>
                        <td style="padding:5px" colspan="3">
                            <textarea name="remark" id="remark" class="easyui-textbox" data-options="multiline:true"
                                      placeholder="暂无备注信息" style="width: 580px; height:40px;"></textarea>
                        </td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" id="saveSerialNumber" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:void(0)" id="cancelSerialNumber" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#serialNumberDlg').dialog('close')">取消</a>
</div>
<div id="batAddSerialNumberDlg" class="easyui-dialog" style="padding:10px 20px" fit="true"
     closed="true" buttons="#bat-dlg-buttons" modal="true" collapsible="false" closable="true">
    <form id="batAddserialNumberFM" method="post" novalidate>
        <div id="batAddtt" class="easyui-tabs" style="width: 100%; height: 520px">
            <div title="批量自定义序列号" style="padding:20px;">
                <table>
                    <tr>
                        <td style="width:60px; height:30px;">药品名称</td>
                        <td style="padding:5px;width:300px;">
                            <input name="materialName" id="batAddMaterialName" class="easyui-textbox" readonly="readonly"
                                   style="width: 180px;"/>
                            <a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id="batLookForSelectMaterial">
                                <span class="l-btn-left"><span class="l-btn-text icon-search l-btn-icon-left" style="height: 20px"></span></span></a>
                        </td>
                    </tr>
                    <tr>
                        <td style="width:80px;">序列号前缀</td>
                        <td style="padding:5px;width:180px;">
                            <input name="serialNumber" id="batAddSerialNumberInput" class="easyui-textbox"
                                   data-options="required:true,validType:'length[1,30]'"
                                   style="width: 180px;"/>
                        </td>
                        <td style="width:80px;">序列号数量</td>
                        <td style="padding:5px;width:180px;">
                            <input name="batAddTotal" id="batAddTotal" class="easyui-numberbox"
                                   data-options="min:0,max:500"
                                   style="width: 180px;"/>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td style="height:30px;">备注</td>
                        <td style="padding:5px" colspan="3">
                            <textarea name="remark" id="batAddremark" class="easyui-textbox" data-options="multiline:true"
                                      placeholder="暂无备注信息" style="width: 580px; height:40px;"></textarea>
                        </td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
    </form>
</div>
<div id="bat-dlg-buttons">
    <a href="javascript:void(0)" id="batAddSerialNumberBtn" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:void(0)" id="batCancelSerialNumberBtn" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#batAddSerialNumberDlg').dialog('close')">取消</a>
</div>

<div id="forSelectMaterialDlg" class="easyui-dialog" style="width:380px;padding:10px 20px"
     closed="true" modal="true" cache="false" collapsible="false" closable="true">
</div>

<script type="text/javascript">
    var url;
    //初始化界面
    $(function () {
        //初始化系统基础信息
        initTableData();
        ininPager();
        initForm();
    });
    //防止表单提交重复
    function initForm() {
        $('#serialNumberFM').form({
            onSubmit: function () {
                return false;
            }
        });
    }
    //初始化表格数据
    function initTableData() {
        //改变宽度和高度
        $("#searchPanel").panel({width: webW - 2});
        $("#tablePanel").panel({width: webW - 2});
        $('#tableData').datagrid({
            //title:'药品列表',
            //iconCls:'icon-save',
            //width:700,
            height: heightInfo,
            nowrap: false,
            rownumbers: false,
            //动画效果
            animate: false,
            //选中单行
            singleSelect: true,
            collapsible: false,
            selectOnCheck: false,
            //fitColumns:true,
            //单击行是否选中
            //checkOnSelect : false,
            pagination: true,
            //交替出现背景
            striped: true,
            //loadFilter: pagerFilter,
            pageSize: initPageSize,
            pageList: initPageNum,
            columns: [[
                {field: 'id', width: 35, align: "center", checkbox: true},
                {field: 'depotheadId', hidden: true},
                {field: 'depotHeadType', hidden: true},
                {
                    title: '操作', field: 'op', align: "center", width: 60, formatter: function (value, rec,index) {
                        var str = '';
                        /**
                         * create by: qiankunpingtai
                         * create time: 2019/3/21 11:23
                         * website：https://qiankunpingtai.cn
                         * description:
                         *  对于每次拼接字符串作为参数传递
                         *  再对字符串分解获取数据的方式，个人感觉效率不是很高，实现也不优雅
                         *  在此使用传递index，然后根据index获取数据的方式替代
                         *
                         */
                        str += '<img title="编辑" src="/js/easyui/themes/icons/pencil.png" style="cursor: pointer;" onclick="editSerialNumber(\'' + index + '\');"/>&nbsp;&nbsp;&nbsp;';
                        str += '<img title="删除" src="/js/easyui/themes/icons/edit_remove.png" style="cursor: pointer;" onclick="deleteSerialNumber(' + rec.id + ');"/>';
                        return str;
                    }
                },
                {title: '药品名称', align: "center",field: 'materialName', width: 140},
                {title: '序列号',align: "center", field: 'serialNumber', width: 200},
                {title: '单据编号', align: "center", field: 'depotHeadNumber', width: 140,
                    formatter: function (value, row) {
                        if(row.depotHeadNumber){
                            return '<a class="n-link" onclick="turnBillDetailPage(\'' + row.depotHeadNumber + '\''+',\'' + row.depotHeadType + '\');">' + row.depotHeadNumber + '</a>';
                        } else {
                            return '';
                        }
                    }
                },
                {title: '已卖出', align: "center", field: 'isSell', width: 60 , formatter: function (value) {
                        return value=='1' ? "是" : "否";
                    }
                },
                {title: '创建时间',align: "center",  field: 'createTime', width: 160,formatter: formatDatebox},
                {title: '更新时间', align: "center", field: 'updateTime', width: 160,formatter: formatDatebox},
                {title: '备注',align: "center",  field: 'remark',width: 200}
            ]],
            toolbar: [
                {
                    id: 'addSerialNumber',
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        addSerialNumber();
                    }
                },
                '-',
                {
                    id: 'batDeleteSerialNumber',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        batDeleteSerialNumber();
                    }
                },
                '-',
                {
                    id: 'batAddSerialNumber',
                    text: '批量添加',
                    iconCls: 'icon-basket_add',
                    handler: function () {
                        batAddSerialNumber();
                    }
                },
            ],
            onLoadError: function () {
                $.messager.alert('页面加载提示', '页面加载异常，请稍后再试！', 'error');
                return;
            }
        });
        dgResize();
        showSerialNumberDetails(1, initPageSize);
    }
    //分页信息处理
    function ininPager() {
        try {
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNum, pageSize) {
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh',
                        {
                            pageNumber: pageNum,
                            pageSize: pageSize
                        });
                    showSerialNumberDetails(pageNum, pageSize);
                },
                onBeforeRefresh:function(pageNum, pageSize){
                    opts.pageSize = pageSize;
                    var pageNumberTemp=parseInt($($("input.pagination-num").get(0)).val());

                    opts.pageNumber = pageNumberTemp;
                    pager.pagination('refresh',
                        {
                            pageNumber: pageNumberTemp,
                            pageSize: pageSize
                        });
                },
            }
            );
        }
        catch (e) {
            $.messager.alert('异常处理提示', "分页信息异常 :  " + e.name + ": " + e.message, 'error');
        }
    }
    //删除序列号信息
    function deleteSerialNumber(id) {
        $.messager.confirm('删除确认', '确定要删除此序列号信息吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: "/serialNumber/batchDeleteSerialNumberByIds",
                    dataType: "json",
                    data: ({
                        ids: id
                    }),
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else {
                            $.messager.alert('删除提示', '删除序列号信息失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('删除提示', '删除序列号信息异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }
    //批量删除序列号
    function batDeleteSerialNumber() {
        var row = $('#tableData').datagrid('getChecked');
        if (row.length == 0) {
            $.messager.alert('删除提示', '没有记录被选中！', 'info');
            return;
        }
        if (row.length > 0) {
            $.messager.confirm('删除确认', '确定要删除选中的' + row.length + '条序列号信息吗？', function (r) {
                if (r) {
                    var ids = "";
                    for (var i = 0; i < row.length; i++) {
                        if (i == row.length - 1) {
                            ids += row[i].id;
                            break;
                        }
                        //alert(row[i].id);
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type: "post",
                        url: "/serialNumber/batchDeleteSerialNumberByIds",
                        dataType: "json",
                        async: false,
                        data: ({
                            ids: ids
                        }),
                        success: function (res) {
                            if(res && res.code === 200) {
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked", false);
                            } else {
                                $.messager.alert('删除提示', '删除序列号信息失败，请稍后再试！', 'error');
                            }
                        },
                        //此处添加错误处理
                        error: function () {
                            $.messager.alert('删除提示', '删除序列号信息异常，请稍后再试！', 'error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //增加
    function addSerialNumber() {
        if(checkPower()){
            return;
        }
        $('#serialNumberDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;增加序列号信息');
        $(".window-mask").css({width: webW, height: webH});
        $('#serialNumberFM').form('clear');
        $('#materialId').val('');
        $('#materialName').val('');
        $('#serialNumberId').val('');
        $('#serialNumber').val('');
        $('#remark').val('');
        url = '/serialNumber/addSerialNumber';
    }
    //检查药品名称和序列号是否存在
    function checkIsExist() {
        var materialName = $.trim($("#materialName").val());
        var serialNumber = $.trim($("#serialNumber").val());
        var id = $.trim($("#serialNumberId").val());
        //开始ajax名称检验，不能重名(新增或编辑)
            $.ajax({
                type: "post",
                url: "/serialNumber/checkIsExist",
                dataType: "json",
                async: false,
                data: ({
                    materialName: materialName,
                    serialNumber: serialNumber,
                    id: id
                }),
                success: function (res) {
                    if(res && res.code != 200) {
                        if(res.msg) {
                                $.messager.alert('提示', res.msg, 'error');
                                return;
                        }
                    }
                },
                //此处添加错误处理
                error: function () {
                    $.messager.alert('提示', '检查序列号信息是否存在异常，请稍后再试！', 'error');
                    return;
                }
            });
        return false;
    }
    //保存信息
    $("#saveSerialNumber").off("click").on("click", function () {
        if (!$('#serialNumber').val()) {
            $.messager.alert('提示', '序列号不能为空！', 'warning');
            return;
        }
        if (checkIsExist()) {
            return;
        }
        //构造序列号json
        var objInfo = $("#serialNumberFM").serializeObject();
        objInfo.id=objInfo.serialNumberId;
        $.ajax({
            type: "post",
            url: url,
            dataType: "json",
            async: false,
            data: ({
                info: JSON.stringify(objInfo)
            }),
            success: function(res) {
                if(res){
                    if(res.code!=200){
                        $.messager.alert('提示', res.msg, 'error');
                        return;
                    }
                    if(res.code === 200) {
                        $('#serialNumberDlg').dialog('close');
                        //加载完以后重新初始化
                        var opts = $("#tableData").datagrid('options');
                        showSerialNumberDetails(opts.pageNumber, opts.pageSize);
                    }

                }

            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '保存序列化信息异常，请稍后再试！', 'error');
                return;
            }
        });
    });
    //编辑信息
    function editSerialNumber(index) {
        //获取当前行
        var rowsdata = $("#tableData").datagrid("getRows")[index];
        $("#serialNumberId").val(rowsdata.id);
        $("#materialId").val(rowsdata.materialId);
        $("#materialName").focus().textbox("setValue", rowsdata.materialName);
        $("#serialNumber").textbox("setValue", rowsdata.serialNumber);
        $("#remark").textbox("setValue", rowsdata.remark);
        $('#serialNumberDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/pencil.png"/>&nbsp;编辑序列号信息');
        $(".window-mask").css({width: webW, height: webH});
        //选中基本资料tab
        $("#serialNumberFM #tt .tabs li").first().click();
        url = '/serialNumber/updateSerialNumber';
    }
    //搜索处理
    $("#searchBtn").unbind().bind({
        click: function () {
            showSerialNumberDetails(1, initPageSize);
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            opts.pageNumber = 1;
            opts.pageSize = initPageSize;
            pager.pagination('refresh', {
                pageNumber: 1,
                pageSize: initPageSize
            });
        }
    });
    function showSerialNumberDetails(pageNo, pageSize) {
        var materialName = $.trim($("#materialName_s").val());
        var serialNumber = $.trim($("#serialNumber_s").val());
        $.ajax({
            type: "get",
            url: "/serialNumber/list",
            dataType: "json",
            data: ({
                search: JSON.stringify({
                    materialName: materialName,
                    serialNumber: serialNumber
                }),
                currentPage: pageNo,
                pageSize: pageSize
            }),
            success: function (res) {
                if(res && res.code === 200){
                    if(res.data && res.data.page) {
                        $("#tableData").datagrid('loadData', res.data.page);
                    }
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('查询提示', '查询数据后台异常，请稍后再试！', 'error');
                return;
            }
        });
    }
    //重置按钮
    $("#searchResetBtn").unbind().bind({
        click: function () {
            $("#materialName_s").textbox("clear");
            $("#serialNumber_s").textbox("clear");
            //加载完以后重新初始化
            $("#searchBtn").click();
        }
    });
    /**时间格式化*/
    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1, // month
            "d+": this.getDate(), // day
            "h+": this.getHours(), // hour
            "m+": this.getMinutes(), // minute
            "s+": this.getSeconds(), // second
            "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
            "S": this.getMilliseconds()
            // millisecond
        }
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        return format;
    }
    function formatDatebox(value) {
        if (value == null || value == '') {
            return '';
        }
        var dt;
        if (value instanceof Date) {
            dt = value;
        } else {
            dt = new Date(value);
        }

        return dt.format("yyyy-MM-dd hh:mm:ss"); //扩展的Date的format方法(上述插件实现)
    }
    /**
     * 批量添加序列号
     * */
    function batAddSerialNumber(){
        $('#batAddSerialNumberDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;批量添加序列号信息');
        $(".window-mask").css({width: webW, height: webH});
        $('#batAddSerialNumberFM').form('clear');
        $('#batAddTotal').textbox("setValue",'10');
        url = '/serialNumber/batAddSerialNumber';
    }
    //保存信息
    $("#batAddSerialNumberBtn").off("click").on("click", function () {
        if (!$('#batAddMaterialName').val()) {
            $.messager.alert('提示', '药品名称不能为空！', 'warning');
            return;
        }
        if (!$('#batAddSerialNumberInput').val()) {
            $.messager.alert('提示', '序列号前缀不能为空！', 'warning');
            return;
        }
        if (!$('#batAddTotal').val()) {
            $.messager.alert('提示', '序列号数量不能为空！', 'warning');
            return;
        }
        var materialName=$('#batAddMaterialName').val();
        var serialNumberPrefix=$('#batAddSerialNumberInput').val();
        var batAddTotal= $('#batAddTotal').val();
        var remark=$('#batAddremark').val();
        $.ajax({
            type: "post",
            url: url,
            dataType: "json",
            async: false,
            data: ({
                materialName:materialName,
                serialNumberPrefix:serialNumberPrefix,
                batAddTotal:batAddTotal,
                remark:remark
            }),
            success: function(res) {
                if(res){
                    if(res.code!=200){
                        $.messager.alert('提示', res.msg, 'error');
                        return;
                    }
                    if(res.code === 200) {
                        $('#batAddSerialNumberDlg').dialog('close');
                        //加载完以后重新初始化
                        var opts = $("#tableData").datagrid('options');
                        showSerialNumberDetails(opts.pageNumber, opts.pageSize);
                    }

                }

            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '保存序列化信息异常，请稍后再试！', 'error');
                return;
            }
        });
    });
    //查询药品名称
    $("#lookForSelectMaterial").on("click", function () {
        $("#selectType").val("lookForSelectMaterial");
        $('#forSelectMaterialDlg').dialog({
            title: '药品名称选择',
            width: webW/2,
            height: webH/2,
            closed: false,
            cache: false,
            href: '/pages/materials/material_forselect.html',
            modal: true,
            resizable:true
        });
    });
    //查询药品名称批量添加
    $("#batLookForSelectMaterial").on("click", function () {
        $("#selectType").val("batLookForSelectMaterial");
        $('#forSelectMaterialDlg').dialog({
            title: '药品名称选择',
            width: webW/2,
            height: webH/2,
            closed: false,
            cache: false,
            href: '/pages/materials/material_forselect.html',
            modal: true,
            resizable:true
        });
    });

</script>
</body>
</html>
